<script setup>
import { useCartStore } from '@/stores/cartStore';
import { createOrderAPI } from "@/apis/checkout";
import { ref } from 'vue';
import {useRouter} from 'vue-router'
import { getPayAPI,getTopUpAPI,getBalanceAPI } from "@/apis/pay.js";
import {getCheckListAPI,deleteOrder} from "@/apis/order";
import { ElMessage } from 'element-plus';
import { useCountDown } from '@/composables/useCountDown';
const {formatTime,start}=useCountDown()
// start(60)
const CartStore = useCartStore()
const SumPrice=ref(0)
SumPrice.value=CartStore.SelectPrice+8
const payForm=ref({
  password:'',
  payMoney:SumPrice.value
})
start(900)
//获取到订单的id
console.log("2321323221321321",CartStore.orderId);

const router=useRouter()
const payFunc=()=>{
  router.push('/member/balance')
}
const payShow=ref(false)

const Balance=ref(0)
const getBalance=async()=>{
    const res=await getBalanceAPI()
  
    Balance.value=res.data.data
    console.log('余额',Balance.value)
  }
  getBalance()

const submitPay=async()=>{
  // payShow.value=true
  if(Balance.value<payForm.value.payMoney)
  {
    ElMessage.error("余额不足,请充值")
    //11
    
    router.push('/member/balance')
  }else{
    const getCheckListRes=await getCheckListAPI(CartStore.orderId)
    console.log('---------------------',getCheckListRes.data.data);
    getCheckListRes.data.data.payStatus=1
    const res1=await createOrderAPI(getCheckListRes.data.data)
    console.log('创建订单',res1.data);
    console.log('支付信息',payForm.value);
    const res=await getPayAPI(payForm.value)
    console.log(res);
    if(res.status===200){
      router.push('/home')//跳首页
    }
    payShow.value = false
    await deleteOrder(CartStore.orderId)
    ElMessage.success("支付成功")
  }
}
</script>

<script>
export default {
  name: 'PayPage',
  setup() {
    return {
      payForm,
      payShow,
      submitPay
    }
  }
}
</script>


<template>
  <div class="xtx-pay-page">
    <div class="container">
      <!-- 付款信息 -->
      <div class="pay-info">
        <span class="icon iconfont icon-queren2"></span>
        <div class="tip">
          <p>订单提交成功！请尽快完成支付。</p>
          <p>支付还剩 <span>{{ formatTime }}</span>, 超时后将取消订单</p>
        </div>
        <div class="amount">
          <span>应付总额：</span>
          <span>¥{{ SumPrice.toFixed(2) }}</span>
        </div>
      </div>
      <!-- 付款方式 -->
      <div class="pay-type">
        <p class="head">选择以下支付方式付款</p>
        <div class="item">
          <p>支付平台</p>
          <a class="btn wx" href="javascript:;"></a>
          <a class="btn alipay" :href="payUrl"></a>
        </div>
        <div class="item">
          <p>支付方式</p>
          <el-button class="btn" @click="payFunc">茶币充值</el-button>
          <el-button class="btn" @click="payShow=true">茶币支付</el-button>
        </div>
      </div>
    </div>
  </div>

  <el-dialog title="茶币支付" v-model="payShow" style="width: 500px;">
    <el-form :model="payForm" label-width="100px">
      <el-form-item label="支付金额：">
        ¥{{ SumPrice.toFixed(2) }}
      </el-form-item>
      <el-form-item label="支付密码：">
        <el-input v-model="payForm.password" type="password" placeholder="支付密码与登录密码一致"></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="payShow = false">取消</el-button>
      <el-button type="primary" @click="submitPay">确认支付</el-button>
    </template>
  </el-dialog>

</template>

<style scoped lang="scss">
.xtx-pay-page {
  margin-top: 20px;
}

.pay-info {

  background: #fff;
  display: flex;
  align-items: center;
  height: 240px;
  padding: 0 80px;

  .icon {
    font-size: 80px;
    color: #1dc779;
  }

  .tip {
    padding-left: 10px;
    flex: 1;

    p {
      &:first-child {
        font-size: 20px;
        margin-bottom: 5px;
      }

      &:last-child {
        color: #999;
        font-size: 16px;
      }
    }
  }

  .amount {
    span {
      &:first-child {
        font-size: 16px;
        color: #999;
      }

      &:last-child {
        color: $priceColor;
        font-size: 20px;
      }
    }
  }
}

.pay-type {
  margin-top: 20px;
  background-color: #fff;
  padding-bottom: 70px;

  p {
    line-height: 70px;
    height: 70px;
    padding-left: 30px;
    font-size: 16px;

    &.head {
      border-bottom: 1px solid #f5f5f5;
    }
  }

  .btn {
    width: 150px;
    height: 50px;
    border: 1px solid #e4e4e4;
    text-align: center;
    line-height: 50px;
    margin-left: 30px;
    color: #666666;
    display: inline-block;

    &.active,
    &:hover {
      border-color: $xtxColor;
    }

    &.alipay {
      background: url(https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/7b6b02396368c9314528c0bbd85a2e06.png) no-repeat center / contain;
    }

    &.wx {
      background: url(https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/c66f98cff8649bd5ba722c2e8067c6ca.jpg) no-repeat center / contain;
    }
  }
}
</style>